#include "apue.h"
#include <pthread.h>

pthread_mutex_t mt;
pthread_t tid1, tid2;
void *tret;
int count = 0;

void *func1(void *arg) {
	while (count < 100) {
		pthread_mutex_lock(&mt);
		sleep(1);
		printf("count = %d\n", ++count);
		pthread_mutex_unlock(&mt);
	}
	pthread_exit((void*)1);
}

void *func2(void *arg) {
	while (count < 100) {
		pthread_mutex_lock(&mt);
		sleep(1);
		printf("count = %d\n", ++count);
		pthread_mutex_unlock(&mt);
	}
	pthread_exit((void*)2);
}

int main(void) {
	pthread_mutex_init(&mt, NULL);
	pthread_create(&tid1, NULL, func1, NULL);
	pthread_create(&tid2, NULL, func2, NULL);
	pthread_join(tid1, &tret);
	pthread_join(tid2, &tret);
	exit(0);
}
